package com.wyzz.hr.util.output;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.wyzz.hr.base.ComonBeans;

public class FormatToString {

	public FormatToString() {
	}

	public FormatToString(String dataType, String format) throws Exception {
		setFormat(dataType, format);
	}

	public String formatToString(Object obj) throws Exception {
		if (obj == null)
			return "";
		if (dataType.compareToIgnoreCase("date") == 0)
			return dateFormatToString(obj);
		if (dataType.compareToIgnoreCase("string") == 0 && num != null)
			return strMaxLen(obj);
		if (dataType.compareToIgnoreCase("decimal") == 0 && num != null)
			return numScaleToString(obj);
		if (explains != null)
			return (String) explains.get(obj.toString());
		else
			return obj.toString();
	}

	public Object format(Object obj) throws Exception {
		if (obj == null)
			return null;
		if (dataType.compareToIgnoreCase("date") == 0)
			return dateFormat(obj);
		if (dataType.compareToIgnoreCase("string") == 0 && num != null)
			return strMaxLen(obj);
		if (dataType.compareToIgnoreCase("decimal") == 0
				|| dataType.compareToIgnoreCase("integer") == 0)
			return numScale(obj);
		if (explains != null)
			return explains.get(obj.toString());
		else
			return obj.toString();
	}

	public String toStringAfterFormat(Object obj) throws Exception {
		if (obj == null)
			return "";
		if (dataType.compareToIgnoreCase("date") == 0)
			return dateFormatToString(obj);
		else
			return obj.toString();
	}

	public String strMaxLen(Object obj) {
		String str = obj.toString();
		return str.length() <= num.intValue() ? str : str.substring(0, num
				.intValue());
	}

	public String numScaleToString(Object obj) throws Exception {
		String objClass = obj.getClass().getName();
		BigDecimal bdNum;
		if (objClass.compareTo("java.math.BigDecimal") == 0)
			bdNum = (BigDecimal) obj;
		else if (objClass.compareTo("java.lang.Double") == 0) {
			Double obj1 = (Double) obj;
			bdNum = BigDecimal.valueOf(obj1.doubleValue());
		} else {
			String result = obj.toString();
			bdNum = new BigDecimal(result);
		}
		bdNum = bdNum.setScale(num.intValue(), 6);
		return bdNum.toString();
	}

	public BigDecimal numScale(Object obj) throws Exception {
		String objClass = obj.getClass().getName();
		BigDecimal bdNum;
		if (objClass.compareTo("java.math.BigDecimal") == 0)
			bdNum = (BigDecimal) obj;
		else if (objClass.compareTo("java.lang.Double") == 0) {
			Double obj1 = (Double) obj;
			bdNum = BigDecimal.valueOf(obj1.doubleValue());
		} else {
			String result = obj.toString();
			bdNum = new BigDecimal(result);
		}
		if (num != null)
			bdNum = bdNum.setScale(num.intValue(), 6);
		return bdNum;
	}

	public String dateFormatToString(Object obj) throws Exception {
		return simpleDateFormat.format((Date) obj);
	}

	public Date dateFormat(Object obj) throws Exception {
		return simpleDateFormat.parse(simpleDateFormat.format((Date) obj));
	}

	public String getDataType() {
		return dataType;
	}

	public boolean setFormat(String dataType, String format) {
		this.dataType = null;
		this.format = null;
		num = null;
		explains = null;
		if (dataType == null || dataType.length() < 1)
			return false;
		if (format == null || format.length() < 1)
			format = null;
		if (dataType.compareToIgnoreCase("string") == 0
				|| dataType.compareToIgnoreCase("decimal") == 0) {
			if (format != null)
				num = new Integer(format);
			this.format = format;
		} else if (dataType.compareToIgnoreCase("date") == 0) {
			simpleDateFormat = new SimpleDateFormat(format);
			this.format = format;
		} else if (dataType.compareToIgnoreCase("integer") == 0) {
			this.format = null;
		} else {
			this.format = null;
			explains = ComonBeans.getValuesToMap(dataType, new boolean[0]);
			if (explains.size() < 1) {
				explains = null;
				return false;
			}
		}
		try {
			this.dataType = dataType;
			return true;
		} catch (Exception e) {
		}
		if (dataType.compareToIgnoreCase("date") == 0)
			this.format = "yyyy-MM-dd";
		else
			this.format = null;
		return false;
	}

	public String getFormat() {
		return format;
	}

	public static final String _STRING = "string";
	public static final String _DECIMAL = "decimal";
	public static final String _DATE = "date";
	public static final String _INTEGER = "integer";
	public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
	private String dataType;
	private String format;
	private SimpleDateFormat simpleDateFormat;
	private Integer num;
	private Map explains;
}
